Skip to content

feat: add before_run and after_run hook points to Agent - #11985

Merged
julian-risch merged 2 commits into
mainfrom
add-before-run-after-run-hook-points
Jul 13, 2026
Merged

feat: add before_run and after_run hook points to Agent#11985
julian-risch merged 2 commits into
mainfrom
add-before-run-after-run-hook-points

Conversation

@julian-risch

@julian-risch julian-risch commented Jul 13, 2026

Copy link
Copy Markdown
Member

Related Issues

Proposed Changes:

Adds before_run and after_run hook points to the Agent:

  • before_run: runs exactly once per run, after the state is initialized and before first LLM call. Use it to rewrite the initial messages or seed state without re-running on every step like before_llm does. Removes the need for the if state.get("brief"): return guard in the agent-pack ScopeHook.
  • after_run: runs exactly once per run, after the step loop has ended and before the Agent builds its return value — regardless of whether the run stopped on an exit condition or because max_agent_steps was reached (unlike on_exit). State mutations are reflected in the returned messages / last_message and state_schema outputs — so a WriteHook-style report writer can no longer be skipped when the step budget is exhausted. Setting continue_run here has no effect.

Implementation details:

  • HookPoint Literal and BEFORE_RUN / AFTER_RUN constants extended in haystack/hooks/protocol.py and exported from haystack.hooks. Validation, serialization, allowed_hook_points, and warm-up/close lifecycle all pick the new points up generically.
  • Hook invocations added to both Agent.run and Agent.run_async, inside the tracing span (so after_run mutations are captured in the output content tag). No new reserved state keys.
  • Docs updated: hooks.mdx and agent.mdx in docs-website, plus the hooks parameter docstring in Agent.__init__.

How did you test it?

  • new unit tests in test/components/agents/test_agent_hooks.py
  • Manual end-to-end script mirroring the deep-research flow

Notes for the reviewer

  • The after_run invocation is placed before _public_outputs(...) so hooks can shape the returned result like the WriteHook in agent-pack.
  • on_exit semantics are unchanged; its doc bullet now cross-references after_run for logic that must run however the run ends.
  • agent-pack https://github.com/deepset-ai/haystack-agent-pack/pull/1: ScopeHook / WriteHook can drop their guards and switch allowed_hook_points to the new points once this ships.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

🤖 Generated with Claude Code

before_run fires once per run after state initialization and before the
first LLM call. after_run fires once per run after the step loop ends,
before the Agent builds its return value, regardless of whether the run
stopped on an exit condition or max_agent_steps (unlike on_exit).

Fixes deepset-ai/haystack-private#455

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@vercel

vercel Bot commented Jul 13, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
haystack-docs Ignored Ignored Preview Jul 13, 2026 12:43pm

Request Review

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/agents
  agent.py
  haystack/hooks
  __init__.py
  protocol.py
Project Total  

This report was generated by python-coverage-comment-action

@julian-risch
julian-risch marked this pull request as ready for review July 13, 2026 12:43
@julian-risch
julian-risch requested a review from a team as a code owner July 13, 2026 12:44
@julian-risch
julian-risch requested review from sjrl and removed request for a team July 13, 2026 12:44

@sjrl sjrl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@julian-risch
julian-risch merged commit 2082333 into main Jul 13, 2026
27 checks passed
@julian-risch
julian-risch deleted the add-before-run-after-run-hook-points branch July 13, 2026 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants